package com.example.learn.controller;

import com.example.learn.domain.IncomeChart;
import com.example.learn.entity.Income;
import com.example.learn.entity.User;
import com.example.learn.service.IncomeService;
import com.example.learn.token.CurrentUser;
import com.example.learn.token.UserLoginToken;
import com.example.learn.utile.Response.BaseResponse;
import com.example.learn.utile.Response.ObjectRestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/income")
public class IncomeController {
    @Autowired
    IncomeService incomeService;

    @UserLoginToken
    @RequestMapping(value = "getincome",method = RequestMethod.GET)
    public BaseResponse getincome(@CurrentUser User user, @RequestParam String queryTime, @RequestParam String queryname){
        List<Income> incomeList;
            if(queryTime.isEmpty()){
                LocalDate today = LocalDate.now();
                DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM");
                queryTime=dateTimeFormatter2.format(today);
            }
            incomeList=    incomeService.getincome(queryTime,queryname,user);

        return new ObjectRestResponse().data(incomeList);
    }

    @UserLoginToken
    @RequestMapping(value = "addincome",method = RequestMethod.POST)
    public BaseResponse addincome(@RequestBody Income income , @CurrentUser User user){
        String incomeStr=      incomeService.addincome(income,user);
        return new ObjectRestResponse().data(incomeStr);
    }

    @RequestMapping(value = "deleteIncome",method = RequestMethod.POST)
    public BaseResponse deleteIncome(String id){
        String incomeStr=      incomeService.deleteIncome(id);
        return new ObjectRestResponse().data(incomeStr);
    }


    @UserLoginToken
    @RequestMapping(value = "topleftmethod",method = RequestMethod.POST)
    public BaseResponse topleftmethod(@CurrentUser User user, @RequestParam String queryTime, @RequestParam String queryname){
        List<IncomeChart> incomeList;
        if(queryTime.isEmpty()){
            LocalDate today = LocalDate.now();
            DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM");
            queryTime=dateTimeFormatter2.format(today);
        }
        incomeList=    incomeService.topleftmethod(queryTime,queryname,user);

        return new ObjectRestResponse().data(incomeList);
    }
    @UserLoginToken
    @RequestMapping(value = "downrightmethod",method = RequestMethod.POST)
    public BaseResponse downrightmethod(@CurrentUser User user,  @RequestParam String queryTime,  @RequestParam String queryname){
        List<String> stringList;
        if(queryTime.isEmpty()){
            LocalDate today = LocalDate.now();
            DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM");
            queryTime=dateTimeFormatter2.format(today);
        }
        stringList=    incomeService.downrightmethod(user,queryname,queryTime);

        return new ObjectRestResponse().data(stringList);
    }
}
